﻿using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.Services;
using ZenSystemService.Api.Entities;
using System.Collections.Generic;
using ZenSystemService.Api.Common;
namespace ZenSystemService.Api.Business
{
    public class DTOBusiness
    {
        private readonly RoleService _roleService;
        private readonly UserRoleService _userRoleService;
        private readonly TraineeOrderService _traineeOrderService;
        private readonly UserService _userService;
        private readonly CourseService _courseService;

        public DTOBusiness(RoleService roleService, UserRoleService userRoleService, TraineeOrderService traineeOrderService, UserService userService,
            CourseService courseService)
        {
            _roleService = roleService;
            _userRoleService = userRoleService;
            _traineeOrderService = traineeOrderService;
            _userService = userService;
            _courseService = courseService;
        }

        /// <summary>
        /// 组织用户输出信息
        /// </summary>
        /// <param name="_user"></param>
        /// <returns></returns>
        public async Task<UserInfoResponse> ReorganizeUserInfoResponseDTO(User _user)
        {
            UserInfoResponse _userInfoResponse = new UserInfoResponse();
            _userInfoResponse.UserId = _user.UserId;
            _userInfoResponse.Username = _user.Username;
            _userInfoResponse.RealName = _user.RealName;
            _userInfoResponse.Mobile = _user.Mobile;
            _userInfoResponse.Gender = _user.Gender;
            _userInfoResponse.AreaID = _user.AreaID;
            _userInfoResponse.Height = _user.Height;
            _userInfoResponse.Weight = _user.Weight;
            _userInfoResponse.Birthday = _user.Birthday;
            _userInfoResponse.AvatarUrl = _user.AvatarUrl;
            _userInfoResponse.Remark = _user.Remark;
            IEnumerable<Role> list = await _userRoleService.GetUserRolesAsync(_user.UserId);
            List<Role> RoleList = null;
            if (list != null)
                RoleList = list.ToList();
            List<RoleDTO> RoleDTOList = new List<RoleDTO>();
            _userInfoResponse.ParentUserId = _user.ParentUserId.HasValue ? _user.ParentUserId.Value : 0;
            string roleName = "";
            if (RoleList != null && RoleList.Count > 0)
            {
                for (int i = 0; i < RoleList.Count; i++)
                {
                    roleName += RoleList[i].DisplayName + ";";
                    RoleDTO _roleDTO = new RoleDTO();
                    _roleDTO.Id = RoleList[i].Id;
                    _roleDTO.Name = RoleList[i].Name;
                    _roleDTO.DisplayName = RoleList[i].DisplayName;
                    RoleDTOList.Add(_roleDTO);
                }
                roleName = roleName.Trim(';');
            }
            _userInfoResponse.RoleList = RoleDTOList;

            //子账号
            if (_user.ParentUserId.HasValue && _user.ParentUserId.Value > 0)
            {
                TraineeCharacterRelation _traineeCharacterRelation =await _userService.GetTraineeCharacterRelationInfo(_user.ParentUserId.Value, _user.UserId);
                if (_traineeCharacterRelation != null)
                    _userInfoResponse.TraineeRelationId = _traineeCharacterRelation.TraineeRelationId;
            }

            //用户剩余课程
            if (_user.UserType == ((int)UserType.Trainee).ToString())
            {
                _userInfoResponse.TraineeCourseSurplusList = await _traineeOrderService.GetTraineeCourseSurplus(_userInfoResponse.UserId);

                if (_userInfoResponse.TraineeCourseSurplusList != null && _userInfoResponse.TraineeCourseSurplusList.Count > 0)
                {
                    if (_user.InstitutionId.HasValue)
                    {
                        IEnumerable<CourseType> coursetypelis = await _courseService.GetCourseTypeListAsync(_user.InstitutionId.Value);
                        List<CourseType> CourseTypeList = null;
                        if (coursetypelis != null)
                            CourseTypeList = coursetypelis.ToList();
                        for (int i = 0; i < _userInfoResponse.TraineeCourseSurplusList.Count; i++)
                        {
                            if (CourseTypeList != null)
                            {
                                CourseType _courseType=CourseTypeList.FirstOrDefault(item => item.CourseTypeId == _userInfoResponse.TraineeCourseSurplusList[i].CourseTypeId);
                                if (_courseType != null)
                                {
                                    _userInfoResponse.TraineeCourseSurplusList[i].CourseType = _courseType.Type;
                                }
                            }
                        }
                    }
                }

            }
            return _userInfoResponse;

        }


        /// <summary>
        /// 组织机构输出信息
        /// </summary>
        /// <param name="institution"></param>
        /// <returns></returns>
        public InstitutionInfoResponse ReorganizeInstitutionResponseDTO(Institution institution)
        {
            InstitutionInfoResponse _institutionInfoResponse = new InstitutionInfoResponse();
            _institutionInfoResponse.InstitutionId = institution.InstitutionId;
            _institutionInfoResponse.InstitutionType = institution.InstitutionType;
            _institutionInfoResponse.Name = institution.Name;
            _institutionInfoResponse.OrganizationCode = institution.OrganizationCode;
            _institutionInfoResponse.Address = institution.Address;
            _institutionInfoResponse.Email = institution.Email;
            _institutionInfoResponse.PhoneNo = institution.PhoneNo;
            _institutionInfoResponse.Representative = institution.Representative;
            _institutionInfoResponse.RepresentativePhoneNo = institution.RepresentativePhoneNo;
            _institutionInfoResponse.RepresentativeTitle = institution.RepresentativeTitle;
            return _institutionInfoResponse;
        }


        public async void JointSearchTraineeOrder(TraineeOrder _traineeOrder, List<User> AllUserList)
        {

            if (AllUserList == null || AllUserList.Count == 0)
                return;

            User _traineeUser = AllUserList.FirstOrDefault(item => item.UserId == _traineeOrder.TraineeId);
            if (_traineeUser != null)
            {
                _traineeOrder.TraineeName = _traineeUser.RealName;
                _traineeOrder.TraineeMobile = _traineeUser.Mobile;
            }

            User _salesUser = AllUserList.FirstOrDefault(item => item.UserId == _traineeOrder.SalesUserId);
            if (_salesUser != null)
            {
                _traineeOrder.SalesUserName = _salesUser.RealName;
            }
        }


        public async void JointSearchTraineeOrderDetail(TraineeOrderDetail _traineeOrderDetail, List<User> AllUserList, List<CourseType> CourseTypeList)
        {
            if (CourseTypeList != null)
            {
                CourseType _courseType = CourseTypeList.FirstOrDefault(item => item.CourseTypeId == _traineeOrderDetail.BuyCourseTypeId);
                if (_courseType != null)
                {
                    _traineeOrderDetail.CourseTypeName = _courseType.Name;
                }
            }

            if (AllUserList != null)
            {
                User _traineeUser = AllUserList.FirstOrDefault(item => item.UserId == _traineeOrderDetail.TraineeId);
                if (_traineeUser != null)
                {
                    _traineeOrderDetail.TraineeName = _traineeUser.RealName;
                }

                User _coachUser = AllUserList.FirstOrDefault(item => item.UserId == _traineeOrderDetail.CoachId);
                if (_coachUser != null)
                {
                    _traineeOrderDetail.CoachName = _coachUser.RealName;
                }
            }
        }
    }

}
